package com.gis3c.online.web.controller;

import com.gis3c.online.entry.AirStation;
import com.gis3c.online.service.AirService;
import com.gis3c.online.service.PollutionSourceService;
import com.gis3c.online.service.SurfaceWaterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.swing.table.TableRowSorter;
import java.util.*;

/**
 * Created by hukekuan on 2018/3/19.
 */
@RestController
@RequestMapping(value = "/api/online")
public class OnLineController {
    @Autowired
    private AirService airService;
    @Autowired
    private SurfaceWaterService surfaceWaterService;
    @Autowired
    private PollutionSourceService pollutionSourceService;

    //======================================空气 start==============================================
    @RequestMapping(value = "/air/stationlist", method = RequestMethod.GET)
    @ResponseBody
    public Object AirStationList(
            @RequestParam(value = "stcode",required = false) String stcode,
            @RequestParam(value = "stationname",required = false) String stationName,
            @RequestParam(value = "aqilevel",required = false) String aqiLevel,
            @RequestParam(value = "mainpollute",required = false) String mainPollute,
            @RequestParam(value = "admincode",required = false) String adminCode){
        Map<String,Object> params = new HashMap<>();
        if(stcode != null && !"".equals(stcode)){
            params.put("stcode",stcode);
        }
        if(stationName != null && !"".equals(stationName)){
            params.put("stationName",stationName);
        }
        if(aqiLevel != null && !"".equals(aqiLevel)){
            params.put("aqiLevel",aqiLevel);
        }
        if(mainPollute != null && !"".equals(mainPollute)){
            params.put("mainPollute",mainPollute);
        }
        if(adminCode != null && !"".equals(adminCode)){
            params.put("adminCode",adminCode.split(","));
        }

        return airService.findAirStationListByParans(params);
    }
    //======================================空气 end==============================================


    //======================================地表水 start============================================
    @RequestMapping(value = "/surfacewater/stationlist", method = RequestMethod.GET)
    @ResponseBody
    public Object SurfaceWaterStationList(
            @RequestParam(value = "stcode",required = false) String stcode,
            @RequestParam(value = "stationname",required = false) String stationName,
            @RequestParam(value = "waterLevel",required = false) String waterLevel,
            @RequestParam(value = "rCode",required = false) String rCode){
        Map<String,Object> params = new HashMap<>();
        if(stcode != null && !"".equals(stcode)){
            params.put("stcode",stcode);
        }
        if(stationName != null && !"".equals(stationName)){
            params.put("stationName",stationName);
        }
        if(waterLevel != null && !"".equals(waterLevel)){
            params.put("waterLevel",waterLevel);
        }
        if(rCode != null && !"".equals(rCode)){
            params.put("rCode",rCode);
        }

        return surfaceWaterService.findSurfaceWaterStationListByParams(params);
    }
    //======================================地表水 end==============================================

    //======================================污染源 start============================================
    @RequestMapping(value = "/ps/pslist", method = RequestMethod.GET)
    @ResponseBody
    public Object PollutionSourceList(
            @RequestParam(value="subtype",required = true) Integer subType,
            @RequestParam(value="pollcode",required = true) Integer pollCode,
            @RequestParam(value = "stcode",required = false) String stCode,
            @RequestParam(value = "stationname",required = false) String stationName,
            @RequestParam(value = "controllevel",required = false) String controlLevel,
            @RequestParam(value = "industrytype",required = false) String industryType,
            @RequestParam(value = "statuscode",required = false) String statusCodes
    ){
        Map<String,Object> queryObject = new HashMap<>();
        if(stCode != null && !"".equals(stCode)){
            queryObject.put("stCode",stCode);
        }
        if(stationName != null && !"".equals(stationName)){
            queryObject.put("stationName",stationName);
        }
        if(controlLevel != null && !"".equals(controlLevel)){
            String[] levels = controlLevel.split(",");
            int sumLevel=0;
            for(String level : levels){
                sumLevel += Integer.parseInt(level);
            }
            queryObject.put("controlLevel",sumLevel);
        }
        if(industryType != null && !"".equals(industryType)){
            queryObject.put("industryType",industryType);
        }
        if(statusCodes != null && !"".equals(statusCodes)){
            String[] statusCodesStr = statusCodes.split(",");
            Set<Integer> statusCodeSet = new HashSet<>();
            for(String statusCode:statusCodesStr){
                statusCodeSet.add(Integer.parseInt(statusCode));
            }
            queryObject.put("statusCodes",statusCodeSet);
        }

        return pollutionSourceService.findStationByType(subType, pollCode,queryObject);
    }

    //======================================污染源 end==============================================
}
